home *** CD-ROM | disk | FTP | other *** search
/ Eagles Nest BBS 8 / Eagles_Nest_Mac_Collection_Disc_8.TOAST / Developer Tools⁄Additions / IntermediatC / Queue #9 / queue 2.c next >
C/C++ Source or Header  |  1990-11-07  |  6KB  |  220 lines

  1. /*
  2.     WRITE A PROGRAM WHICH DOES WHAT IS DESCRIBED BELOW.
  3.     YOU ARE GIVEN THE TOP HALF OF THIS PROGRAM [UP TO main()].
  4.     
  5.     The program must call simulation() TIME_LIMIT times.  In this simulation,
  6.     the arrival and service time is decremented.  If arrival reaches 0, add a 
  7.     ship and generate a new arrival time.  Be careful, the new time could be 0
  8.     too.  If this occurs, the new ship must also be added.  Do the same for 
  9.     service, which removes the ship at the head of the queue.  A ship is added
  10.     to the end of the queue and removed from the beginning of the queue.
  11.     
  12.     An variable will track the number of ships on the queue.  No more ships can be
  13.     added is the variable count reaches QUEUE_SIZE.  A variable also points to 
  14.     the head and tail of the queue.  A ship is to be removed by incrementing the head
  15.     variable (so it points to the next ship in line).  If either head or tail reaches
  16.     the end of the queue, it wraps to the beginning (so that it is always possible
  17.     to have QUEUE_SIZE ships in the queue).
  18.     
  19.     Each ship will have a different hull number which is created by incrementing 
  20.     new_number.  The name is a random combination of 3 character arrays.  Generate 3
  21.     random numbers and combine the 3 resulting names (using the numbers as subscripts
  22.     for the strings) into a single name and give that name to the ship.  For example,
  23.     if the 3 random numbers are 2, 3, and 0, the resulting ship name is 
  24.     Exxon America I.
  25.     
  26.     Display the ship list each time a ship is added.
  27. */
  28.  
  29. /*
  30.  *
  31.  * This is a program to study a circular queue of ships
  32.  * waiting to pass through a lock.  Each ship is created
  33.  * at some random arrival interval and placed on the queue.
  34.  * At other random intervals, the queue is serviced and the
  35.  * first ship in line is removed.  In theory, removal would
  36.  * transfer the ship to the lock, but in this problem the ship
  37.  * is just destroyed.
  38.  * The ships on the queue are displayed each time a new ship arrives.
  39.  *
  40.  */
  41.  
  42.  
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <stdlib.h>
  46.  
  47.  
  48. #define QUEUE_SIZE        10        /* maximum number of waiting ships */
  49. #define NAME_SIZE        40        /* length of each ship's name */
  50. #define MAX_ARRIVAL        12        /* maximum arrival interval */
  51. #define MAX_SERVICE        24        /* maximum service interval */
  52. #define TIME_LIMIT        100        /* run of simulation (0 to TIME_LIMIT) */
  53. #define random(x)        (rand() % (x) )
  54.  
  55.  
  56. typedef struct ship_type
  57.    {
  58.    int hull_number;
  59.    char name[NAME_SIZE];
  60.    } ship_type;
  61.    
  62.    
  63. typedef struct queue_type
  64.    {
  65.    int count;
  66.    int head;
  67.    int tail;
  68.    ship_type ship[QUEUE_SIZE];
  69.    } queue_type;
  70.    
  71.    
  72. static void simulation (void);                /* called by main TIME_LIMIT times */
  73. static ship_type create_ship (void);        /* returns a new ship */
  74. static void add_ship (ship_type new_ship);    /* adds ship to queue */
  75. static void remove_ship (void);                /* removes by changing head */
  76. static void display_queue (void);            /* lists ships on queue, followed by newline */
  77.  
  78.  
  79. static queue_type queue;
  80. static int clock;                /* clock goes from 0 to TIME_LIMIT */
  81. static int arrival;                /* next arrival interval */
  82. static int service;                /* next service interval */
  83. static FILE * file_stream;        /* file control block pointer */
  84. static int new_number = 101;    /* hull numbers for newly created ships */
  85. static char * first[] =
  86.    {
  87.    "U. S. S. ",
  88.    "HMS ",
  89.    "Exxon ",
  90.    "Yacht ",
  91.    "SS ",
  92.    "Royal ",
  93.    "Tug "
  94.    };
  95. static char * second[] =
  96.    {
  97.    "Dreadnaught ",
  98.    "Valdez ",
  99.    "Annie ",
  100.    "America ",
  101.    "Scotland ",
  102.    "Ille de France ",
  103.    "Voyager "
  104.    };
  105. static char * third[] =
  106.    {
  107.    "I",
  108.    "II",
  109.    "III",
  110.    "IV",
  111.    "V",
  112.    "VI",
  113.    "VII"
  114.    };
  115.  
  116.  
  117. /* ------------------------------------------------------------------------------- */
  118.  
  119.  
  120. main()
  121.    {
  122.    file_stream = fopen("ship queue.out", "w");
  123.    arrival = random(MAX_ARRIVAL);
  124.    service = random(MAX_SERVICE);
  125.    for (clock = 0; clock < TIME_LIMIT; clock++)
  126.       {
  127.       simulation();
  128.       arrival--;
  129.       service--;
  130.       }
  131.    fclose(file_stream);
  132.    }
  133.  
  134.  
  135. /* ------------------------------------------------------------------------------- */
  136.  
  137.  
  138. static void simulation (void)
  139.    {
  140.    ship_type new_ship;
  141.    
  142.    while ((!arrival) || (!service))
  143.       {
  144.       if (!arrival)
  145.          {
  146.          if (queue.count < QUEUE_SIZE)
  147.             {
  148.             new_ship = create_ship();
  149.             add_ship( new_ship );
  150.             display_queue();
  151.             }
  152.          arrival = random(MAX_ARRIVAL);
  153.          }
  154.       if (!service)
  155.          {
  156.          if (queue.count > 0)
  157.             remove_ship();
  158.          service = random(MAX_SERVICE);
  159.          }
  160.       }
  161.    }
  162.  
  163.  
  164. /* ------------------------------------------------------------------------------- */
  165.  
  166.  
  167. static ship_type create_ship (void)
  168.    {
  169.    ship_type new_ship;
  170.    
  171.    new_ship.hull_number = new_number++;
  172.    strcpy( (char *) new_ship.name, (char *) first[random(7)] );
  173.    strcat( (char *) new_ship.name, (char *) second[random(7)] );
  174.    strcat( (char *) new_ship.name, (char *) third[random(7)] );
  175.    return new_ship;
  176.    }
  177.  
  178.  
  179. /* ------------------------------------------------------------------------------- */
  180.  
  181.  
  182. static void add_ship (ship_type new_ship)
  183.    {
  184.    queue.count++;
  185.    queue.ship[queue.tail] = new_ship;
  186.    if ((++queue.tail) >= QUEUE_SIZE)
  187.       queue.tail = 0;
  188.    }
  189.  
  190.  
  191. /* ------------------------------------------------------------------------------- */
  192.  
  193.  
  194. static void remove_ship (void)
  195.    {
  196.    queue.count--;
  197.    if ((++queue.head) >= QUEUE_SIZE)
  198.       queue.head = 0;
  199.    }
  200.  
  201.  
  202. /* ------------------------------------------------------------------------------- */
  203.  
  204.  
  205. static void display_queue (void)
  206.    {
  207.    int i;
  208.    int temp;
  209.    
  210.    for (i = 0, temp = queue.head; i < queue.count; i++)
  211.       {
  212.       fprintf(file_stream, "%4d      %s\n", queue.ship[temp].hull_number,
  213.          (char *) queue.ship[temp].name);
  214.       temp++;
  215.       if (temp >= QUEUE_SIZE)
  216.          temp = 0;
  217.       }
  218.    fprintf(file_stream, "\n");
  219.    }
  220.